package com.deer.mvptest.ui.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.preference.PreferenceGroup;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.deer.mvptest.R;
import com.deer.mvptest.service.entity.Phone;
import com.deer.mvptest.service.presenter.PhonePresenter;
import com.deer.mvptest.service.presenter.TaskPresenter;
import com.deer.mvptest.service.view.OperationView;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Main2Activity extends AppCompatActivity {

    private TaskPresenter presenter = new PhonePresenter();
    private ProgressDialog progressDialog;
    private ArrayAdapter<Phone> arrayAdapter;

    @BindView(R.id.noPhone)
    TextView noPhone;
    @BindView(R.id.btnCreate)
    Button btnCreate;
    @BindView(R.id.listView)
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        ButterKnife.bind(this);
        Logger.addLogAdapter(new AndroidLogAdapter());

        presenter.onCreate();
        presenter.attachView(operationView);

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, presenter.getPhonesList());
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                presenter.removePhone(position);
            }
        });

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.addRandomPhone();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onStop();
    }

    private OperationView operationView = new OperationView() {
        @Override
        public void showCreatingPhone() {
            progressDialog = new ProgressDialog(Main2Activity.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(true);
            progressDialog.setMessage("正在生产手机");
            Logger.d("正在生产手机");
            progressDialog.show();
        }

        @Override
        public void showPhoneCountChange() {
            arrayAdapter.notifyDataSetChanged();
            Logger.d(presenter.getPhonesList());
        }

        @Override
        public void showNoPhone() {
            noPhone.setVisibility(View.VISIBLE);
        }

        @Override
        public void showFactoryBusy() {
            Toast.makeText(Main2Activity.this, "工厂繁忙，请稍后再试！", Toast.LENGTH_SHORT).show();
            Logger.d("工厂繁忙，请稍后再试！");
        }

        @Override
        public void showCreatedPhone() {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            progressDialog = null;
            Toast.makeText(Main2Activity.this, "新生产出一台手机！", Toast.LENGTH_SHORT).show();
            Logger.d("新生产出一台手机！");
        }

        @Override
        public void hideNoPhone() {
            noPhone.setVisibility(View.GONE);
        }
    };
}
